package com.h3c.nem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eos.common.util.IdWorker;
import com.h3c.nem.dto.TbOfferArchieveDTO;
import com.h3c.nem.dto.TbOfferPatentDTO;
import com.h3c.nem.dto.TbOfferPatentInfoDTO;
import com.h3c.nem.entity.TbOfferArchieve;
import com.h3c.nem.mapper.TbOfferPatentMapper;
import com.h3c.nem.entity.TbOfferPatent;
import com.h3c.nem.mapstruct.TbOfferArchieveMapstruct;
import com.h3c.nem.mapstruct.TbOfferPatentMapstruct;
import com.h3c.nem.service.ITbOfferArchieveService;
import com.h3c.nem.service.ITbOfferPatentService;
import com.h3c.nem.utils.AppBusinessException;
import com.h3c.nem.utils.CommonUtil;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 专利信息实现类
 *
 * @author chenyifan
 * @since 2022-06-06 16:34:45
 */
@Service
@AllArgsConstructor
public class TbOfferPatentServiceImpl extends ServiceImpl<TbOfferPatentMapper, TbOfferPatent> implements ITbOfferPatentService {

    private final ITbOfferArchieveService archieveService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOfferPatentInfo(TbOfferPatentInfoDTO patentInfoDTO) {
        if (Objects.nonNull(patentInfoDTO.getArchieveDTOS()) && patentInfoDTO.getArchieveDTOS().size() > 0) {
            for (TbOfferArchieveDTO archieveDTO : patentInfoDTO.getArchieveDTOS()) {
                if (StringUtils.isBlank(archieveDTO.getArchieveName())) {
                    throw new AppBusinessException("成果名称不能为空");
                }
            }
        }
        if (Objects.nonNull(patentInfoDTO.getPatentDTOS()) && patentInfoDTO.getPatentDTOS().size() > 0) {
            for (TbOfferPatentDTO patentDTO : patentInfoDTO.getPatentDTOS()) {
                if (StringUtils.isBlank(patentDTO.getPatentName())) {
                    throw new AppBusinessException("专利名称不能为空");
                }
            }
        }
        remove(new QueryWrapper<TbOfferPatent>().lambda()
                .eq(TbOfferPatent::getWorkerId, CommonUtil.getActivUser().getUserId())
        );
        archieveService.remove(new QueryWrapper<TbOfferArchieve>().lambda()
                .eq(TbOfferArchieve::getWorkerId, CommonUtil.getActivUser().getUserId())
        );
        List<TbOfferPatent> tbOfferPatents = TbOfferPatentMapstruct.INSTANCE.DTOList2EntityList(patentInfoDTO.getPatentDTOS());
        tbOfferPatents.forEach(s -> s.setPatentId(IdWorker.nextId() + "")
                .setWorkerId(CommonUtil.getActivUser().getUserId())
                .setUpdateTime(new Date()));
        saveBatch(tbOfferPatents);
        List<TbOfferArchieve> archieveList = TbOfferArchieveMapstruct.INSTANCE.DTOList2EntityList(patentInfoDTO.getArchieveDTOS());
        archieveList.forEach(s -> s.setArchieveId(IdWorker.nextId() + "")
                .setWorkerId(CommonUtil.getActivUser().getUserId())
                .setUpdateTime(new Date()));
        archieveService.saveBatch(archieveList);
    }

    @Override
    public Map<String, Object> showOfferPatentInfo() {
        HashMap<String, Object> result = new HashMap<>(2);
        List<TbOfferPatent> patentList = list(new QueryWrapper<TbOfferPatent>().lambda()
                .eq(TbOfferPatent::getWorkerId, CommonUtil.getActivUser().getUserId())
        );
        List<TbOfferArchieve> archieveList = archieveService.list(new QueryWrapper<TbOfferArchieve>().lambda()
                .eq(TbOfferArchieve::getWorkerId, CommonUtil.getActivUser().getUserId())
        );
        result.put("patentList", patentList);
        result.put("archieveList", archieveList);
        return result;
    }

    @Override
    public List<TbOfferPatent> getOfferPatentInfoByWorkerId(String workerId) {
        QueryWrapper<TbOfferPatent> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TbOfferPatent::getWorkerId, workerId);
        return list(queryWrapper);
    }
}

